home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / libx11.lha / libX11 / x11display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-22  |  48.6 KB  |  1,789 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      display
  20.    PURPOSE
  21.      add display handling to libX11
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Oct 23, 1994: Created.
  26. ***/
  27.  
  28. #include <time.h>
  29.  
  30. #include <intuition/intuition.h>
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <proto/gadtools.h>
  34.  
  35. #include <graphics/gfxbase.h>
  36. #include <graphics/gfxmacros.h>
  37.  
  38. #include <libraries/asl.h>
  39. #include <proto/asl.h>
  40.  
  41. #include <dos.h>
  42. #include <signal.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45.  
  46. #include "libX11.h"
  47. #define XLIB_ILLEGAL_ACCESS 1
  48.  
  49. #include <X11/Xutil.h>
  50. #include <X11/Intrinsic.h>
  51. /*
  52. #include <X11/IntrinsicP.h>
  53. #include <X11/CoreP.h>
  54.  
  55. #include <X11/Xlibint.h>
  56. */
  57.  
  58. #include <libraries/mui.h>
  59. #include <proto/muimaster.h>
  60.  
  61. #include "amigax_proto.h"
  62. #include "amiga_x.h"
  63.  
  64. ListNode_t *pMemoryList=NULL;
  65.  
  66. #ifdef XMUIAPP
  67. struct Library *MUIMasterBase=NULL;
  68. #endif
  69.  
  70. /*******************************************************************************************/
  71. /* globals */
  72. /*******************************************************************************************/
  73.  
  74. int amiga_id=-1,amiga_ham8=0;
  75. int Xdash;
  76. UWORD *XOrigPattern;
  77. byte XOrigPatternSize;
  78. WORD *Xcurrent_tile=NULL;
  79.  
  80. Display amigaX_display;
  81. Screen  amiga_screen[2];
  82. extern Visual X11AmigaVisual[2];
  83. /*Screen  screens[10];*/
  84. GC      amiga_gc;
  85.  
  86. Window prevwin=NULL;
  87. GC prevgc=NULL;
  88. struct RastPort *drp;
  89.  
  90.  
  91. #define MAX_COORD 400 /* 100 vertices, 5 bytes each = 500 bytes. */ 
  92.  
  93. X11userdata *Xuserdata=NULL;
  94. X11userdata *XBackUserData=NULL;
  95. struct AreaInfo *_Xoldarea;
  96. struct TmpRas *_Xoldtmpras;
  97.  
  98. void X11free_userdata(X11userdata *ud);
  99.  
  100. /* internal tile based filling */
  101.  
  102. int X11FillFree=0;
  103. Pixmap X11FillSource=0;
  104. int X11InternalFill=0;
  105. struct BitMap *X11FillBitMap;
  106.  
  107. /******/
  108.  
  109. int userdata_width(void) {
  110.   if (Xuserdata) return Xuserdata->AWidth;
  111.   return 0;
  112. }
  113.  
  114. int userdata_height(void) {
  115.   if (Xuserdata) return Xuserdata->AHeight;
  116.   return 0;
  117. }
  118.  
  119.  
  120. /*******************************************************************************************/
  121. /* externals */
  122. /*******************************************************************************************/
  123. /*struct Library    *IconBase = NULL;*/
  124. #define NEEDINTUITIONBASE
  125. #define NEEDGFXBASE
  126.  
  127. #ifdef NEEDINTUITIONBASE
  128. extern struct IntuitionBase *IntuitionBase;
  129. #endif
  130. #ifdef NEEDGFXBASE
  131. extern struct GfxBase *GfxBase;
  132. #endif
  133. extern struct Library *AslBase;
  134. extern struct Library *GadToolsBase;
  135. extern struct Library *DiskfontBase;
  136. extern struct Library *LayersBase;
  137. extern struct Library *KeymapBase;
  138.  
  139. /*
  140. extern struct Library *CxBase;
  141. extern struct Library *UtilityBase;
  142. */
  143. extern struct DosLibrary *DOSBase;
  144.  
  145. struct Screen *Scr=NULL,*wb=NULL;
  146. struct RastPort temprp,backrp,backfillrp,backfillrp2;
  147. extern int prevcm;
  148.  
  149. char *LIBversion="$VER: libX11 1.00 (03.24.94)";
  150.  
  151. char *LibX11Info="This program is using libX11 by Terje Pedersen!";
  152.  
  153. struct Layer_Info *X11layerinfo;
  154.  
  155. struct Window *rootwin=NULL;
  156.  
  157. /*******************************************************************************************/
  158. /* defines */
  159. /*******************************************************************************************/
  160. #define SCALE8TO32(x) ((x)|((x)<<8)|((x)<<16)|((x)<<24))
  161.  
  162. #define SCREENOPENFAIL 10L
  163. #define SCREENVISUALFAIL 11L
  164. #define SCRWINOPENFAIL 12L
  165. #define ACWFAIL 20L
  166. #define XCSFAIL 30L
  167.  
  168. /*******************************************************************************************/
  169. /* global  data */
  170. /*******************************************************************************************/
  171.  
  172. DisplayGlobals_s DG = { 0 };
  173.  
  174. char vendor[]="Amiga",*X11cwd=NULL;
  175. int borderadj=0,adjx,adjy;
  176. int newpic=1; /* remove it..*/
  177.  
  178. /*
  179. int new_id=0;
  180. int maxwidth=720,maxheight=560;
  181. int df=0;
  182. */
  183. int usewb=1,/*Adisplay=0,*/wbapp=0;
  184. int X_relx,X_rely,X_bottom,X_right,X_width,X_height;
  185. int debugxemul=0,gfxcard=0,askmode=0;
  186.  
  187. /*struct TextAttr topaz8 = {(STRPTR)"topaz.font",8,0x00,0x01};*/
  188. UWORD DriPens[] = { 65535 };
  189. APTR VisualInfo = NULL;
  190. ULONG wbmode=DEFAULT_MONITOR_ID;
  191.  
  192. struct RastPort drawrp;
  193.  
  194. /*X11winchild children[CHILDRENAVAIL];*/
  195. /*long amigamask[CHILDRENAVAIL];*/
  196.  
  197. struct NameInfo dbuffer;
  198. char pbuffer[MAXPUBSCREENNAME]="";
  199.  
  200. int X11NumDrawables,X11NumDrawablesWindows,X11NumDrawablesBitmaps;
  201. int X11NumDrawablesSubWindows,X11NumMUI;
  202.  
  203. int X11AvailDrawables,X11AvailWindows,X11AvailBitmaps;
  204. int X11AvailSubWindows,X11AvailMUI,X11AvailGC,X11NumGC;
  205. GC *X11GC;
  206.  
  207. char *X11Drawables,*X11DrawablesBackground;
  208. int *X11DrawablesMap,*X11DrawablesWindowsInvMap;
  209. long *X11DrawablesMask;
  210. struct Window **X11DrawablesWindows;
  211. X11BitMap_t *X11DrawablesBitmaps;
  212. Object **X11DrawablesMUI;
  213. Cursor *X11DrawablesMUICursor;
  214. _ActualWindow *X11ActualWindows;
  215. int *X11DrawablesSubWindows;
  216. X11winchild *X11DrawablesChildren;
  217.  
  218. /*******************************************************************************************/
  219.  
  220. void X11resource_exit(int n){
  221.   printf("Unable to allocate internal resources! (%d)\n",n);
  222.   exit(-1);
  223. }
  224.  
  225. /*
  226. typedef struct {
  227.   int entries,maxentries,entrysize;
  228.   char *data;
  229. } DMap;
  230.  
  231. NewDMap(DMap *pm){
  232.   char *new=malloc(pm->entrysize*(pm->entries+5));
  233.   if(!new) X11resource_exit(-1);
  234.   if(pm->data!=NULL){
  235.     memcpy(new,pm->date,pm->entries*pm->entrysize);
  236.     free(pm->data);
  237.   }
  238.   pm->maxentries=pm->entries+5;
  239.   pm->data=new;
  240. }
  241.  
  242. XID NewCmapsEntry(struct ColorMap *cmap){
  243.   X11Cmaps[X11NumCmaps++]=cmap;
  244.   if(X11NumCmaps==X11AvailCmaps){
  245.     X11Cmaps=NewPointerMap(X11AvailCmaps+10,X11Cmaps,X11AvailCmaps);
  246.     X11AvailCmaps+=10;
  247.   }
  248.   return(X11NumCmaps-1);
  249. }
  250. */
  251.  
  252. void X11init_drawables(void){
  253.   X11NumDrawables=1; X11AvailDrawables=2;
  254.   X11NumDrawablesWindows=0;
  255.   X11NumDrawablesBitmaps=0;
  256.   X11NumDrawablesSubWindows=0;
  257.   X11NumMUI=0;
  258.   X11AvailWindows=2;
  259.   X11AvailBitmaps=2;
  260.   X11AvailSubWindows=2;
  261.   X11AvailMUI=2;
  262.   X11AvailGC=2;
  263.   X11NumGC=0;
  264.  
  265.   X11Drawables=(char*)calloc(X11AvailDrawables,1);
  266.   X11DrawablesBackground=(char*)calloc(X11AvailDrawables,1);
  267.   X11DrawablesMap=(int*)calloc(X11AvailDrawables*sizeof(int),1);
  268.   X11DrawablesWindowsInvMap=(int*)malloc(X11AvailWindows*sizeof(int));
  269.   X11DrawablesMask=(long*)malloc(X11AvailDrawables*sizeof(long));
  270.   X11ActualWindows=(_ActualWindow*)calloc(1,X11AvailWindows*sizeof(_ActualWindow));
  271.   X11DrawablesWindows=(struct Window**)calloc(X11AvailWindows*sizeof(struct Window*),1);
  272.   X11DrawablesBitmaps=(X11BitMap_t*)malloc(X11AvailBitmaps*sizeof(X11BitMap_t));
  273.   X11DrawablesMUI=(Object**)malloc(X11AvailMUI*sizeof(Object*));
  274.   X11DrawablesMUICursor=(Cursor*)malloc(X11AvailMUI*sizeof(Cursor));
  275.   X11DrawablesSubWindows=(int*)malloc(X11AvailSubWindows*sizeof(int));
  276.   X11DrawablesChildren=(X11winchild*)calloc(X11AvailSubWindows*sizeof(X11winchild),1);
  277.   X11GC=(GC*)malloc(X11AvailGC*sizeof(int *));
  278.   if(!X11Drawables||!X11DrawablesMap||!X11DrawablesWindows||!X11DrawablesBitmaps||
  279.      !X11DrawablesSubWindows||!X11DrawablesWindowsInvMap||!X11DrawablesMask||
  280.      !X11DrawablesMUI||!X11DrawablesChildren||!X11DrawablesMUICursor||!X11GC)
  281.     X11resource_exit(DISPLAY1);
  282.   X11Drawables[0]=X11ROOT;
  283. }
  284.  
  285. void X11exit_drawables(void){
  286.   free(X11DrawablesChildren);
  287.   free(X11Drawables);
  288.   free(X11DrawablesBackground);
  289.   free(X11DrawablesMap);
  290.   free(X11DrawablesMask);
  291.   free(X11ActualWindows);
  292.   free(X11DrawablesWindowsInvMap);
  293.   free(X11DrawablesWindows);
  294.   free(X11DrawablesBitmaps);
  295.   free(X11DrawablesSubWindows);
  296.   free(X11DrawablesMUI);
  297.   free(X11DrawablesMUICursor);
  298.   free(X11GC);
  299. }
  300.  
  301. void X11expand_drawables(void){
  302.   char *old=X11Drawables;
  303.   char *oldback=X11DrawablesBackground;
  304.   int *oldmap=X11DrawablesMap;
  305.   long *oldmask=X11DrawablesMask;
  306.   X11Drawables=(char*)malloc(X11AvailDrawables+10);
  307.   X11DrawablesBackground=(char*)calloc(X11AvailDrawables+10,1);
  308.   X11DrawablesMap=(int*)malloc((X11AvailDrawables+10)*sizeof(int));
  309.   X11DrawablesMask=(long*)malloc((X11AvailDrawables+10)*sizeof(long));
  310.  
  311.   if(!X11Drawables||!X11DrawablesMap||!X11DrawablesMask) X11resource_exit(DISPLAY2);
  312.   memcpy(X11Drawables,old,X11AvailDrawables);
  313.   memcpy(X11DrawablesBackground,oldback,X11AvailDrawables);
  314.   memcpy(X11DrawablesMap,oldmap,X11AvailDrawables*sizeof(int));
  315.   memcpy(X11DrawablesMask,oldmask,X11AvailDrawables*sizeof(long));
  316.   X11AvailDrawables+=10;
  317.   free(old);
  318.   free(oldback);
  319.   free(oldmap);
  320.   free(oldmask);
  321. }
  322.  
  323. void X11expand_windows(void){
  324.   struct Window **old=X11DrawablesWindows;
  325.   int *oldwin=X11DrawablesWindowsInvMap;
  326.   _ActualWindow *oldactual=X11ActualWindows;
  327.   X11DrawablesWindows=(struct Window**)calloc((X11AvailWindows+10)*sizeof(struct Window*),1);
  328.   X11DrawablesWindowsInvMap=(int*)calloc((X11AvailWindows+10)*sizeof(int),1);
  329.   X11ActualWindows=(_ActualWindow*)calloc((X11AvailWindows+10)*sizeof(_ActualWindow),1);
  330.   memcpy(X11DrawablesWindows,old,X11AvailWindows*sizeof(struct Window*));
  331.   memcpy(X11DrawablesWindowsInvMap,oldwin,X11AvailWindows*sizeof(int));
  332.   memcpy(X11ActualWindows,oldactual,X11AvailWindows*sizeof(_ActualWindow));
  333.   if(!X11DrawablesWindows||!X11DrawablesWindowsInvMap||!X11ActualWindows) X11resource_exit(DISPLAY3);
  334.   X11AvailWindows+=10;
  335.   free(old);
  336.   free(oldwin);
  337.   free(oldactual);
  338. }
  339.  
  340. void X11expand_bitmaps(void){
  341.   X11BitMap_t *old=X11DrawablesBitmaps;
  342.   X11DrawablesBitmaps=(X11BitMap_t*)calloc((X11AvailBitmaps+10)*sizeof(X11BitMap_t),1);
  343.   if(!X11DrawablesBitmaps) X11resource_exit(DISPLAY4);
  344.   memcpy(X11DrawablesBitmaps,old,X11AvailBitmaps*sizeof(X11BitMap_t));
  345.   X11AvailBitmaps+=10;
  346.   free(old);
  347. }
  348.  
  349. void X11expand_MUI(void){
  350.   Object **old=X11DrawablesMUI;
  351.   Cursor *oldc=X11DrawablesMUICursor;
  352.   X11DrawablesMUI=(Object**)malloc((X11AvailMUI+10)*sizeof(Object *));
  353.   X11DrawablesMUICursor=(Cursor*)malloc((X11AvailMUI+10)*sizeof(Cursor));
  354.   if(!X11DrawablesMUI||!X11DrawablesMUICursor) X11resource_exit(DISPLAY4);
  355.   memcpy(X11DrawablesMUI,old,X11AvailMUI*sizeof(Object*));
  356.   memcpy(X11DrawablesMUICursor,oldc,X11AvailMUI*sizeof(Cursor));
  357.   X11AvailMUI+=10;
  358.   free(old);
  359.   free(oldc);
  360. }
  361.  
  362. void X11expand_subwindows(void){
  363.   int *old=X11DrawablesSubWindows;
  364.   X11winchild *oldchild=X11DrawablesChildren;
  365.   X11DrawablesSubWindows=(int*)calloc((X11AvailSubWindows+10)*sizeof(int),1);
  366.   X11DrawablesChildren=(X11winchild*)calloc((X11AvailSubWindows+10)*sizeof(X11winchild),1);
  367.   if(!X11DrawablesSubWindows||!X11DrawablesChildren) X11resource_exit(DISPLAY5);
  368.   memcpy(X11DrawablesSubWindows,old,X11AvailSubWindows*sizeof(int));
  369.   memcpy(X11DrawablesChildren,oldchild,X11AvailSubWindows*sizeof(X11winchild));
  370.   X11AvailSubWindows+=10;
  371.   free(old);
  372.   free(oldchild);
  373. }
  374.  
  375. void X11expand_GC(void){
  376.   GC *old=X11GC;
  377.   X11GC=(GC*)malloc((X11AvailGC+10)*sizeof(int*));
  378.   if(!X11GC) X11resource_exit(DISPLAY5);
  379.   memcpy(X11GC,old,X11AvailGC*sizeof(int*));
  380.   X11AvailGC+=10;
  381.   free(old);
  382. }
  383.  
  384. X11NewWindow(struct Window *win){
  385.   X11Drawables[X11NumDrawables]=X11WINDOW;
  386.   X11DrawablesMap[X11NumDrawables]=X11NumDrawablesWindows;
  387.   X11DrawablesWindowsInvMap[X11NumDrawablesWindows]=X11NumDrawables++;
  388.   X11DrawablesWindows[X11NumDrawablesWindows++]=win;
  389.   if(X11NumDrawables==X11AvailDrawables) X11expand_drawables();
  390.   if(X11NumDrawablesWindows==X11AvailWindows) X11expand_windows();
  391.   return(X11NumDrawables-1);
  392. }
  393.  
  394. int X11NewGC(GC newGC){
  395.   X11GC[X11NumGC++]=newGC;
  396.   if(X11NumGC==X11AvailGC) X11expand_GC();
  397.   return(X11NumGC-1);
  398. }
  399.  
  400. X11NewBitmap( struct BitMap *bmp, int width, int height, int depth ){
  401.   X11Drawables[X11NumDrawables]=X11BITMAP;
  402.   X11DrawablesMap[X11NumDrawables++]=X11NumDrawablesBitmaps;
  403.   X11DrawablesBitmaps[X11NumDrawablesBitmaps].width=width;
  404.   X11DrawablesBitmaps[X11NumDrawablesBitmaps].height=height;
  405.   X11DrawablesBitmaps[X11NumDrawablesBitmaps].depth=depth;
  406.   X11DrawablesBitmaps[X11NumDrawablesBitmaps].bTileStipple=0;
  407.   X11DrawablesBitmaps[X11NumDrawablesBitmaps++].pBitMap=bmp;
  408.   if(X11NumDrawables==X11AvailDrawables) X11expand_drawables();
  409.   if(X11NumDrawablesBitmaps==X11AvailBitmaps) X11expand_bitmaps();
  410.   return(X11NumDrawables-1);
  411. }
  412.  
  413. Window X11NewMUI(Object *obj){
  414.   X11Drawables[X11NumDrawables]=X11MUI;
  415.   X11DrawablesMap[X11NumDrawables++]=X11NumMUI;
  416.   X11DrawablesMUI[X11NumMUI++]=obj;
  417.   if(X11NumDrawables==X11AvailDrawables) X11expand_drawables();
  418.   if(X11NumMUI==X11AvailMUI) X11expand_MUI();
  419.   return((Window)(X11NumDrawables-1));
  420. }
  421.  
  422. X11SetMui(XID window, Object* obj){
  423.   X11DrawablesMUI[X11DrawablesMap[window]]=obj;
  424. }
  425.  
  426. X11NewSubWindow(int win){
  427.   X11Drawables[X11NumDrawables]=X11SUBWINDOW;
  428.   X11DrawablesMap[X11NumDrawables++]=X11NumDrawablesSubWindows;
  429.   X11DrawablesSubWindows[X11NumDrawablesSubWindows++]=win;
  430.   if(X11NumDrawables==X11AvailDrawables) X11expand_drawables();
  431.   if(X11NumDrawablesSubWindows==X11AvailSubWindows) X11expand_subwindows();
  432.   return(X11NumDrawables-1);
  433. }
  434.  
  435. X11OldSubWindow(int win){
  436.   int i;
  437.   for( i=0; i>X11NumDrawables; i++ )
  438.     if( X11Drawables[i]==X11SUBWINDOW && X11DrawablesSubWindows[X11DrawablesMap[i]]==win ){
  439.       return i;
  440.     }
  441.   return 0;
  442. }
  443.  
  444. /*******************************************************************************************/
  445.  
  446. /* PTINRECT returns '1' if x,y is in rect (inclusive) */
  447. #define PTINRECT(x,y,rx,ry,rw,rh) \
  448.            ((x)>=(rx) && (y)>=(ry) && (x)<=(rx)+(rw) && (y)<=(ry)+(rh))
  449.  
  450. int check_inside_subwindows(struct Window *win,int x,int y){
  451.   int i;
  452.   for(i=DG.nNumChildren;i>0;i--){
  453.     if(PTINRECT(x,y,X11DrawablesChildren[i].x,X11DrawablesChildren[i].y,X11DrawablesChildren[i].width,X11DrawablesChildren[i].height)&&
  454.        !X11DrawablesChildren[i].deleted&&X11DrawablesChildren[i].mapped&&
  455.        X11DrawablesChildren[i].parent==EG.nEventDrawable){
  456.       X_relx=win->BorderLeft+X11DrawablesChildren[i].x;
  457.       X_rely=win->BorderTop+X11DrawablesChildren[i].y;
  458. /*      printf("inside subwindow %d\n",i);*/
  459.       return(i);
  460.     }
  461.   }
  462. /*
  463.   X_relx=0;
  464.   X_rely=0;
  465. */
  466.  
  467.   return(0);
  468. }
  469.  
  470. int X11findparent(int win){
  471.   int pos;
  472.   if(X11Drawables[win]==X11WINDOW) return(win);
  473.   pos=X11DrawablesChildren[X11DrawablesSubWindows[X11DrawablesMap[win]]].parent;
  474.   if(X11Drawables[pos]!=X11SUBWINDOW){
  475. /*    printf("findparent: %d is a parent of %d\n",pos,win);*/
  476.     return(pos);
  477.   } else return(X11findparent(pos));
  478. }
  479.  
  480. void clear_subwin(Window win,int border,int background){
  481.   struct Window *w;
  482.   int child=X11DrawablesSubWindows[X11DrawablesMap[win]];
  483.   int parent=X11findparent(win);
  484.   if(win!=prevwin) if(!(drp=setup_win(win))) return;
  485.   w=Agetwin(win);
  486.   prevwin=-1;
  487.   prevgc=(GC)-1;
  488.   if(!w) return;
  489.   if(!X11ActualWindows[X11DrawablesMap[parent]].mapped) return;
  490.  
  491.   SetDrMd(drp,JAM1);
  492.   SetAPen(drp,X11DrawablesBackground[win]);
  493.   RectFill(drp,
  494.        w->BorderLeft+X11DrawablesChildren[child].x+border,
  495.        w->BorderTop+X11DrawablesChildren[child].y+border,
  496.        w->BorderLeft+X11DrawablesChildren[child].x+X11DrawablesChildren[child].width-border,
  497.        w->BorderTop+X11DrawablesChildren[child].y+X11DrawablesChildren[child].height-border);
  498. }
  499.  
  500. /*******************************************************************************************/
  501.  
  502. void force_exit(n){
  503. /*  printf("forced exit!\n");*/
  504.   signal(SIGINT,SIG_DFL); 
  505.   signal(SIGABRT,SIG_DFL);
  506.   XFlush(&amigaX_display);
  507.   rootwin=NULL;
  508.   XCloseDisplay(&amigaX_display);
  509.   exit(-1);
  510. }
  511.  
  512. /*******************************************************************************************/
  513.  
  514. void report_display(void){
  515.   if(debugxemul){
  516.     printf("dosbase lib version %d\n",DOSBase->dl_lib.lib_Version);
  517.     if(!DG.bUse30) printf("not ");
  518.     printf("using os3.0 funcs.\n");
  519. /*
  520. #ifdef _M68040
  521.     printf("using 68040 code\n");
  522. #elif defined(_M68030)
  523.     printf("using 68030 code\n");
  524. #elif defined(_M68020)
  525.     printf("using 68020 code\n");
  526. #elif defined(_M68000)
  527.     printf("using 68000 code\n");
  528. #endif
  529.  
  530. #ifdef _M68881
  531.     printf("and 68881 math!\n");
  532. #endif
  533. */
  534.   }
  535. }
  536.  
  537. cantopen(char *lib){
  538.   printf("unable to open %s\n",lib);
  539. }
  540.  
  541. /*struct IOStdReq ioreq;*/
  542.  
  543. int OpenLibraries(void){
  544.   if(DG.bLibsOpen)return(1);
  545.   KeymapBase = OpenLibrary("keymap.library", 37);
  546.   if (KeymapBase == NULL) return FALSE;
  547. #ifdef NEEDGFXBASE
  548.   if (!(GfxBase = (struct GfxBase*)OpenLibrary( "graphics.library", 39L ))){
  549.     if (!(GfxBase = (struct GfxBase*)OpenLibrary( "graphics.library", 37L ))){
  550.       cantopen("graphics.library v39");
  551.       return FALSE;
  552.     }
  553.   }
  554. #endif
  555. #ifdef NEEDINTUITIONBASE
  556.   if (!(IntuitionBase = (struct IntuitionBase*)OpenLibrary( "intuition.library", 39L ))){
  557.     if (!(IntuitionBase = (struct IntuitionBase*)OpenLibrary( "intuition.library", 37L ))){
  558.       cantopen("Intuition.library v37 or v39");
  559.       return FALSE;
  560.     }
  561.     DG.bUse30=0;
  562.   }else DG.bUse30=1;
  563. #endif
  564.   if (!(LayersBase = OpenLibrary( "layers.library", 39L ))){
  565.     if (!(LayersBase = OpenLibrary( "layers.library", 37L ))){
  566.       cantopen("layers.library v37");
  567.       return FALSE;
  568.     }
  569.   }
  570. /*
  571.   if (!(CxBase = OpenLibrary( "commodities.library", 37l ))) return FALSE;
  572.   if (!(IconBase = OpenLibrary( "icon.library", 37l ))) return FALSE;
  573.   if (!(UtilityBase = OpenLibrary( "utility.library", 37l ))) return FALSE;
  574. */
  575.   if (!(AslBase = OpenLibrary( "asl.library", 37L ))){
  576.     cantopen("asl.library v37");
  577.     return FALSE;
  578.   }
  579.   if (!(DiskfontBase = OpenLibrary( "diskfont.library", 36L ))){
  580.     cantopen("asl.library v36");
  581.     return FALSE;
  582.   }
  583.   if (!(GadToolsBase = OpenLibrary( "gadtools.library", 39L ))){
  584.     if (!(GadToolsBase = OpenLibrary( "gadtools.library", 37L ))){
  585.       cantopen("gadtools.library");
  586.       return FALSE;
  587.     }
  588.   }
  589. #ifdef XMUIAPP
  590.   if(!(MUIMasterBase=OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN))) exit(20);
  591. #endif
  592.   DG.bLibsOpen=1;
  593.   return TRUE;
  594. }
  595.  
  596. void CloseLibraries(void){
  597.   if(GadToolsBase)CloseLibrary(GadToolsBase);
  598.   if(DiskfontBase)CloseLibrary(DiskfontBase);
  599.   if(AslBase)CloseLibrary(AslBase);
  600. /*
  601.   if(UtilityBase)CloseLibrary(UtilityBase);
  602.   if(IconBase)CloseLibrary(IconBase);
  603.   if(CxBase)CloseLibrary(CxBase);
  604. */
  605.   if(LayersBase)CloseLibrary((struct Library*)LayersBase);
  606. #ifdef NEEDGFXBASE
  607.   if(GfxBase)CloseLibrary((struct Library*)GfxBase);
  608. #endif
  609. #ifdef NEEDINTUITIONBASE
  610.   if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase);
  611. #endif
  612.   CloseLibrary(KeymapBase);
  613.  
  614. #ifdef XMUIAPP
  615.   if(MUIMasterBase)CloseLibrary(MUIMasterBase);
  616. #endif
  617. }
  618.  
  619. /*******************************************************************************************/
  620. /* 2.04 friendly alloc of bitmap */
  621. /* BMF_CLEAR|BMF_DISPLAYABLE */
  622. struct BitMap *alloc_bitmap(int width,int height,int depth,int flags){
  623.   int i;
  624.   struct BitMap *bmp;
  625. /*  printf("Allocating <%d,%d> depth %d\n",width,height,depth); */
  626.   if(!DG.bUse30){
  627.     bmp=calloc(sizeof(struct BitMap),1);
  628.     if(!bmp) return(0);
  629.     InitBitMap(bmp,depth,width,height);
  630.     for(i=0;i<depth;i++){
  631.       bmp->Planes[i]=(PLANEPTR)AllocRaster(width,height);
  632.       if(!bmp->Planes[i]) X11resource_exit(DISPLAY6);
  633.       memset(bmp->Planes[i],0,RASSIZE(width,height));
  634.     }
  635. /*    bmp->Rows=height;*/
  636.   }else{
  637.     if(!usewb)
  638.       bmp = AllocBitMap((UWORD)(width), (UWORD)height, (UWORD)depth, (UWORD)BMF_CLEAR, NULL);
  639.     else
  640.       bmp = AllocBitMap((UWORD)(width), (UWORD)height, (UWORD)depth, (UWORD)(flags), NULL);
  641.   }
  642.   if(!bmp){
  643.     printf("alloc bitmap %d %d %d failed!\n",width,height,bmp);
  644.     XCloseDisplay(NULL);
  645.     exit(-1);
  646.   }
  647.   for( i=depth; i<8; i++ )
  648.     bmp->Planes[i]=NULL;
  649.   return(bmp);
  650. }
  651.  
  652. int free_bitmap(struct BitMap *bmp){
  653.   int i;
  654.   if(!bmp) return;
  655.   if(!DG.bUse30){
  656.     for(i=0;i<bmp->Depth;i++){
  657.       if(bmp->Planes[i])
  658.     FreeRaster(bmp->Planes[i],(bmp->BytesPerRow)*8,bmp->Rows);
  659.     }
  660.     free(bmp);
  661.   }else FreeBitMap(bmp);
  662. }
  663.  
  664. /*******************************************************************************************/
  665.  
  666. char *getdisplayname(ULONG id){
  667.   int res=GetDisplayInfoData(FindDisplayInfo(id),(UBYTE*)&dbuffer,sizeof(dbuffer),DTAG_NAME,NULL);
  668.   if(res) return((char*)dbuffer.Name);
  669.   return(NULL);
  670. }
  671.  
  672. /*******************************************************************************************/
  673. /*
  674. char X11home[128]="";
  675. */
  676.  
  677. void X11gethome(void);
  678.  
  679. void X11gethome(void){
  680.   X11cwd=getcwd(NULL,128);
  681. /*
  682.   strcpy(X11home,X11cwd);
  683.   {
  684.     h=getenv("X11HOME");
  685.     if(h!=NULL){
  686.       strcpy(X11home,h);
  687.       free(h);
  688.     }
  689.   }
  690. */
  691. }
  692.  
  693. void stackcheck(int n){
  694.   if(stacksize()+stackused()<n){
  695.     printf("set stack to >=%d! current %ld used %ld\n",n,stacksize(),stackused());
  696.     CloseLibraries();
  697.     exit(-1);
  698.   }
  699. }
  700.  
  701. void init_memlist(void) {
  702.   pMemoryList=List_MakeNull();
  703. }
  704.  
  705. void cleanup_memlist(void) {
  706.   List_FreeList(pMemoryList);
  707.   MWReport("At end of main()", MWR_FULL);  /* Generate a memory usage report */
  708. }
  709.  
  710. void init_backfill( int w, int h ){
  711.   init_area(NULL,200,w,h);
  712.   XBackUserData=Xuserdata;
  713.   Xuserdata=NULL;
  714. }
  715.  
  716. void exit_backfill(void){
  717.   X11free_userdata(XBackUserData);
  718. }
  719.  
  720. Display *XOpenDisplay(char * display_name){
  721.   XrmValue value;
  722.   XrmDatabase db;
  723.   struct DimensionInfo dinfo;
  724. /*  struct DisplayInfo buffer;*/
  725. /*  struct MonitorInfo minfo;*/
  726.   DisplayInfoHandle handle;
  727.   char dispname[80]="";
  728.   if(DG.bX11Open) return(&amigaX_display);
  729.  
  730.   memset(&DG,0,sizeof(DisplayGlobals_s));
  731.  
  732.   DG.bX11Cursors=1;
  733.   DG.bX11Open=1;
  734.   DG.nDisplayDepth=8;
  735.   DG.bClearMemList=True;
  736.  
  737.   if(!pMemoryList)
  738.     init_memlist();
  739.   else DG.bClearMemList=False;
  740.  
  741. #ifdef DEBUGXEMUL_ENTRY
  742.   printf("(display)XOpenDisplay [");
  743.   if(display_name) printf("%s]\n",display_name);
  744.   else printf("NULL]\n");
  745. #endif
  746.   if(!OpenLibraries()) X11resource_exit(DISPLAY7);
  747.   fprintf(stderr,LibX11Info);
  748.   fprintf(stderr,"\n");
  749.   stackcheck(20000);
  750.   if(display_name){
  751.     strcpy(dispname,display_name);
  752.     usewb=1;
  753.   }
  754.   XrmInitialize();
  755.   db=XtDatabase(&amigaX_display);
  756.   if(XrmGetResource(db,"Amiga.debug",NULL,NULL,&value))
  757.     debugxemul=1;
  758.   if(XrmGetResource(db,"Amiga.nochunkytoplanar",NULL,NULL,&value))
  759.     gfxcard=1;
  760.   if(XrmGetResource(db,"Amiga.askmode",NULL,NULL,&value))
  761.     askmode=1;
  762.   if(XrmGetResource(db,"Amiga.cursors",NULL,NULL,&value))
  763.     sscanf(value.addr,"%d",&DG.bX11Cursors);
  764.   if(XrmGetResource(db,"Amiga.usepubscreen",NULL,NULL,&value)){
  765.     strcpy(dispname,value.addr);
  766.   }
  767.   if(strlen(dispname)==0) GetDefaultPubScreen(pbuffer);
  768.   else {
  769.     strcpy(pbuffer,dispname);
  770.     if(stricmp(dispname,"Workbench")==0)usewb=1;
  771.   }
  772.   if(strlen(pbuffer)>0){
  773.     if(debugxemul) printf("locking defpub '%s'\n",pbuffer);
  774.     wb=LockPubScreen(pbuffer);
  775.   }
  776.   if(!wb) wb=LockPubScreen(NULL);
  777.  
  778. /*  if(IntuitionBase->lib_Version>=39) DG.bUse30=1; else DG.bUse30=0;*/ /*DOSBase*/
  779.   handle=FindDisplayInfo(HIRES_KEY);
  780.   GetDisplayInfoData(handle,(UBYTE*)&dinfo,sizeof(dinfo),DTAG_DIMS,NULL);
  781.   if(debugxemul)
  782.     printf("display_info max [%d,%d] depth %d.\n",
  783.        dinfo.MaxRasterWidth,dinfo.MaxRasterHeight,dinfo.MaxDepth);
  784.  
  785.   if(dinfo.MaxDepth>8){
  786.     DG.nDisplayDepth=8;
  787.     DG.nDisplayMaxDepth=8;
  788.   }else{
  789.     DG.nDisplayDepth=dinfo.MaxDepth;
  790.     DG.nDisplayMaxDepth=dinfo.MaxDepth;
  791.   }
  792.  
  793.   DG.nDisplayWidth=wb->Width/*dinfo.MaxRasterWidth*/-32;
  794.   DG.nDisplayHeight=wb->Height /*dinfo.MaxRasterHeight*/-16;
  795.   DG.nDisplayMaxWidth=dinfo.MaxRasterWidth;
  796.   DG.nDisplayMaxHeight=dinfo.MaxRasterHeight;
  797.   if(DG.nDisplayMaxWidth>2048) DG.nDisplayMaxWidth=2048;
  798.   if(DG.nDisplayMaxHeight>2048) DG.nDisplayMaxHeight=2048;
  799.  
  800.   X11init_drawables();
  801.  
  802.   amiga_screen[0].cmap=(Colormap)wb->ViewPort.ColorMap;
  803. /*  amiga_screen[1].cmap=XCreateColormap(NULL,NULL,0,256);*/
  804.   DG.nDisplayColors=(1<<(wb->RastPort.BitMap->Depth));
  805.   DG.nDisplayDepth=wb->RastPort.BitMap->Depth;
  806.   if(usewb||wbapp){
  807.     Scr=wb; 
  808.     DG.nDisplayDepth=(wb->RastPort.BitMap->Depth);
  809.     DG.nDisplayMaxDepth=(wb->RastPort.BitMap->Depth);
  810.     DG.nDisplayWidth=wb->Width;
  811.     DG.nDisplayHeight=wb->Height;
  812.     DG.nDisplayMaxWidth=DG.nDisplayWidth;
  813.     DG.nDisplayMaxHeight=DG.nDisplayHeight; 
  814.     X11init_cmaps();
  815.     if(!DG.bWbSaved) savewbcm();
  816.   } else X11init_cmaps();
  817.  
  818.   init_backfill(wb->Width,wb->Height);
  819.  
  820.   if(XrmGetResource(XtDatabase(&amigaX_display),"Amiga.displaydepth",NULL,NULL,&value)){
  821.     sscanf(value.addr,"%d",&DG.nDisplayDepth);
  822.     DG.nDisplayMaxDepth=DG.nDisplayDepth;
  823.   }
  824.   report_display();
  825.  
  826.   amigaX_display.screens=amiga_screen;
  827.   amigaX_display.bitmap_bit_order=1;
  828.   amigaX_display.bitmap_unit=16;
  829.   amiga_screen[0].display=&amigaX_display;
  830.   amiga_screen[1].display=&amigaX_display;
  831.   amigaX_display.vendor=vendor;
  832.   amigaX_display.release=001;
  833.   amigaX_display.display_name=vendor;
  834.   amigaX_display.default_screen=0;
  835.   amiga_screen[0].root_visual=&X11AmigaVisual[0];
  836.   amiga_screen[1].root_visual=&X11AmigaVisual[1];
  837.   {
  838.     XGCValues xgc;
  839.     xgc.foreground=1;
  840.     xgc.background=0;
  841.     amiga_screen[0].default_gc=XCreateGC(&amigaX_display,NULL,GCForeground|GCBackground,&xgc);
  842.   }
  843.   amiga_screen[0].root=ROOTID;
  844.   amiga_screen[0].root_depth=DG.nDisplayDepth;
  845.   amiga_screen[0].width=DG.nDisplayWidth;
  846.   amiga_screen[0].height=DG.nDisplayHeight;
  847.   amiga_screen[0].mwidth=(int)((DG.nDisplayWidth/72)*25.4);
  848.   amiga_screen[0].mheight=(int)((DG.nDisplayHeight/72)*25.4);
  849.   amiga_screen[0].white_pixel=2;
  850.   amiga_screen[0].black_pixel=1;
  851.   amiga_screen[1].max_maps=2;
  852.   X11SetupVisual();
  853. /*
  854.   amiga_visual.map_entries=1<<DG.nDisplayDepth;
  855.   amiga_visual.red_mask=0x800;
  856.   amiga_visual.class=PseudoColor;
  857.   amiga_visual.green_mask=0x080;
  858.   amiga_visual.blue_mask=0x008;
  859.   amiga_visual.bits_per_rgb=8;
  860. */
  861.   signal(SIGINT,force_exit); 
  862.   signal(SIGABRT,SIG_IGN);
  863. /*
  864.   for(i=0;i<CHILDRENAVAIL;i++){
  865.     X11DrawablesChildren[i].x=0;
  866.     X11DrawablesChildren[i].y=0;
  867.     X11DrawablesChildren[i].deleted=0;
  868.     X11DrawablesChildren[i].mapped=0;
  869.   }*/
  870.   open_timer();
  871.   backrp.BitMap=NULL;
  872.   temprp.BitMap=NULL;
  873.  
  874.  
  875.   InitRastPort(&drawrp);
  876. /*
  877.   X11layerinfo=NewLayerInfo();
  878.   {
  879.     struct Screen *oldscr=Scr;
  880.     Scr=wb;
  881.     init_backrp();
  882.     Scr=oldscr;
  883.   }
  884.   drawrp.Layer=CreateUpfrontLayer(X11layerinfo,backrp.BitMap,0,0,wb->Width,wb->Height,LAYERSIMPLE);*/
  885.   amiga_gc=amiga_screen[0].default_gc;
  886.   amiga_gc->values.font=(unsigned long)wb->RastPort.Font;
  887.   if(X11cwd==NULL) X11gethome();
  888.   X11init_fonts();
  889.   X11init_drawing();
  890. /*  for(i=0;i<CHILDRENAVAIL;i++) amigamask[i]=0xFFFF;*/
  891.   chdir(X11cwd);
  892.   X11init_clipping();
  893.   X11init_resources();
  894.   X11init_cursors();
  895.   X11init_events();
  896.   return(&amigaX_display);
  897. }
  898.  
  899. void XSetPlanes(int d){
  900.   amiga_screen[0].root_depth=d;
  901.   DG.nDisplayDepth=d;
  902. /*  amiga_visual.map_entries=1<<DG.nDisplayDepth;*/
  903. }
  904.  
  905. /*******************************************************************************************/
  906.  
  907. /* int num_windows=0;Window windows[10];*/
  908.  
  909. XCloseDisplay(Display * display){
  910.   int i;
  911. #ifdef DEBUGXEMUL_ENTRY
  912.   printf("(display)XCloseDisplay\n");
  913. #endif
  914.   if(!DG.bX11Open) return;
  915.   for(i=0;i<X11NumDrawablesWindows;i++)
  916.     if(X11ActualWindows[i].mapped){
  917.       /* nasty, still not closed */
  918.       XUnmapWindow(NULL,X11ActualWindows[i].win);
  919.     }
  920.   for(i=0;i<X11NumGC;i++)
  921.     if(X11GC[i]!=NULL) XFreeGC(NULL,X11GC[i]);
  922.   for(i=0;i<X11NumDrawablesBitmaps;i++)
  923.     if(X11DrawablesBitmaps[X11DrawablesMap[i]].pBitMap!=NULL) XFreePixmap(NULL,i);
  924.   DG.bX11Open=0;
  925.   if(strlen(pbuffer)>0){
  926.     if(debugxemul) printf("unlocking defpub '%s'\n",pbuffer);
  927.     if(strcmp(pbuffer,"Workbench")!=0)UnlockPubScreen(pbuffer,NULL);
  928.   }
  929.   UnlockPubScreen("Workbench",NULL);
  930.   CloseDownScreen();
  931. /*
  932.   if(drawrp.Layer){
  933.     unclipWindow(drawrp.Layer);
  934.     DeleteLayer(drawrp.Layer);
  935.     DisposeLayerInfo(X11layerinfo);
  936.   }*/
  937. /*  XFreeColormap(NULL,amiga_screen[1].cmap);*/
  938.   {
  939.     extern char *_Xresources;
  940.     if(_Xresources!=NULL) XrmDestroyDatabase((XrmDatabase)_Xresources);
  941.   }
  942. /*
  943.   {
  944.     extern Cursor prevdefined;
  945.     if(prevdefined!=NULL) XFreeCursor(NULL,prevdefined);
  946.   }
  947. */
  948.   signal(SIGINT,SIG_IGN); 
  949.   signal(SIGABRT,SIG_IGN);
  950.  
  951. /*
  952.   for(i=1;i<num_windows;i++)
  953.     if(windows[i]){
  954.       exit_area(windows[i]);
  955.       CloseWindow(windows[i]);
  956.       windows[i]=NULL;
  957.     }
  958. */
  959.   if(Xuserdata)
  960.     X11free_userdata(Xuserdata);
  961.  
  962.   if(DG.bNeedBackRP) free_bitmap(backrp.BitMap);
  963.   if(temprp.BitMap) free_bitmap(temprp.BitMap);
  964.   X11exit_fonts();
  965.   X11exit_drawing();
  966.   chdir(X11cwd);
  967.   free(X11cwd);
  968.   X11cwd=NULL;
  969.  
  970.   unlink("t:rgb.txt");
  971.   close_timer();
  972.   for(i=0;i<X11NumDrawablesBitmaps;i++){
  973.     if(X11DrawablesBitmaps[i].pBitMap!=NULL) free_bitmap(X11DrawablesBitmaps[i].pBitMap);
  974.   }
  975.   X11exit_clipping();
  976.   X11exit_resources();
  977.   X11exit_drawables();
  978.   X11exit_cursors();
  979.   X11exit_events();
  980.   X11exit_cmaps();
  981.   exit_backfill();
  982.   if(X11FillFree) free_bitmap(X11FillBitMap);
  983.   CloseLibraries();
  984.   if(DG.bClearMemList)
  985.     cleanup_memlist();
  986. }
  987.  
  988. /*******************************************************************************************/
  989.  
  990. void setup_bitmap(Window win){
  991.     X_relx=0;X_rely=0;
  992.     drawrp.BitMap=X11DrawablesBitmaps[X11DrawablesMap[win]].pBitMap;
  993.     X_width=X11DrawablesBitmaps[X11DrawablesMap[win]].width;
  994.     X_height=X11DrawablesBitmaps[X11DrawablesMap[win]].height;
  995.     if(Xuserdata!=0){
  996.       drawrp.TmpRas=&(Xuserdata->win_tmpras);
  997.       drawrp.AreaInfo=&(Xuserdata->win_AIstruct);
  998.     } else {
  999.       printf("no tmpras info\n");
  1000.     }
  1001.     drp=&drawrp;
  1002. }
  1003.  
  1004. void setup_tempwin(Window win){
  1005.   int child=X11DrawablesSubWindows[X11DrawablesMap[win]];
  1006.   setup_bitmap(X11ActualWindows[X11DrawablesMap[X11findparent(win)]].pixmap);
  1007.   X_relx=X11DrawablesChildren[child].x; /* relative drawing position */
  1008.   X_rely=X11DrawablesChildren[child].y;
  1009.   X_right=X_relx+X11DrawablesChildren[child].width;
  1010.   X_bottom=X_rely+X11DrawablesChildren[child].height;
  1011. }
  1012.  
  1013. struct Window *Agetwin(Window win){
  1014.   struct Window *w;
  1015.   XRectangle r;
  1016.   if(X11Drawables[win]==X11SUBWINDOW){ /* this is a child */
  1017.     int parent;
  1018.     int child=X11DrawablesSubWindows[X11DrawablesMap[win]];
  1019.     parent=X11findparent(win);
  1020.     w=X11DrawablesWindows[X11DrawablesMap[parent]];
  1021.     if(!w) {
  1022. #if 0
  1023.       return NULL;
  1024. #else
  1025.       XMapWindow(NULL,parent);
  1026.       w=X11DrawablesWindows[X11DrawablesMap[parent]];
  1027. #endif
  1028. /*      setup_tempwin(win);*/
  1029.     }
  1030. /*
  1031.     amiga_screen[0].root=parent;
  1032. */
  1033.     X11DrawablesChildren[0].id=amiga_screen[0].root;
  1034.     X_relx=w->BorderLeft+X11DrawablesChildren[child].x; /* relative drawing position */
  1035.     X_rely=w->BorderTop+X11DrawablesChildren[child].y;
  1036.     X_right=X_relx+X11DrawablesChildren[child].width;
  1037.     X_bottom=X_rely+X11DrawablesChildren[child].height;
  1038.     X_width=X11DrawablesChildren[child].width;
  1039.     X_height=X11DrawablesChildren[child].height;
  1040. /*
  1041.     r.x=X_relx-1;r.y=X_rely-1;r.width=X11DrawablesChildren[child].width+2;r.height=X11DrawablesChildren[child].height+2;
  1042. */
  1043.     r.x=0;r.y=0;r.width=X11DrawablesChildren[child].width;r.height=X11DrawablesChildren[child].height;
  1044.     if(r.x<0) r.x=0;
  1045.     if(r.y<0) r.y=0;
  1046.     amiga_screen[0].root=parent;
  1047.     XSetClipRectangles(&amigaX_display,NULL,0,0,&r,1,0);
  1048.     amiga_screen[0].root=amiga_screen[0].root;
  1049.     prevwin=win;
  1050.     return(w);
  1051.   }else if(X11Drawables[win]==X11BITMAP) {
  1052.     setup_bitmap(win);
  1053.   } else {
  1054.     w=X11DrawablesWindows[X11DrawablesMap[win]];
  1055.     if(!w){
  1056. #if 0
  1057.       return NULL;
  1058. #else
  1059.       XMapWindow(NULL,win);
  1060.       w=X11DrawablesWindows[X11DrawablesMap[win]];
  1061.       if(!w){
  1062.     printf("where is %d\n",win);
  1063.     XCloseDisplay(NULL);
  1064.     exit(-1);
  1065.       }
  1066. #endif
  1067.     }
  1068.     X11DrawablesChildren[0].id=amiga_screen[0].root;
  1069.     X_relx=w->BorderLeft;
  1070.     X_rely=w->BorderTop;
  1071.     X_right=w->Width;
  1072.     X_bottom=w->Height;
  1073.     X_width=w->Width;
  1074.     X_height=w->Height;
  1075. /*
  1076.     r.x=X_relx;r.y=X_rely;
  1077. */
  1078.     r.x=0;r.y=0;
  1079.     r.width=w->Width-w->BorderRight-w->BorderLeft;
  1080.     r.height=w->Height-w->BorderTop-w->BorderBottom;
  1081.     amiga_screen[0].root=win;
  1082.     XSetClipRectangles(&amigaX_display,NULL,0,0,&r,1,0);
  1083.     amiga_screen[0].root=amiga_screen[0].root;
  1084.     prevwin=win;
  1085.     return(w);
  1086.   }
  1087. }
  1088.  
  1089. int SetupScreen(  int wide,int high,int depth, ULONG id){
  1090.   struct DimensionInfo di;
  1091.   int overscan,xpos,ypos;
  1092.   if(usewb) {
  1093.     usewb=0;
  1094.     wbapp=0;
  1095. /*
  1096.     if(Adisplay==19||Adisplay==21) DG.nDisplayMaxDepth=8;
  1097.     else DG.nDisplayMaxDepth=5;*/
  1098.   }
  1099.   else if(depth>DG.nDisplayMaxDepth)depth=DG.nDisplayMaxDepth;
  1100.   if (GetDisplayInfoData(NULL,(UBYTE *)&di,sizeof(di),DTAG_DIMS,id)){
  1101.     int maxwidth= di.VideoOScan.MaxX - di.VideoOScan.MinX + 1;
  1102.     int maxheight= di.VideoOScan.MaxY - di.VideoOScan.MinY + 1;
  1103.   
  1104.     int stdwidth= di.TxtOScan.MaxX - di.TxtOScan.MinX + 1;
  1105.     int stdheight= di.TxtOScan.MaxY - di.TxtOScan.MinY + 1;
  1106.     if (wide > maxwidth||high > maxheight+16){
  1107.       overscan= OSCAN_VIDEO;
  1108.       xpos= (maxwidth  - wide)/ 2;
  1109.       ypos= (maxheight - high)/ 2;
  1110.     }else{
  1111.       overscan= OSCAN_TEXT;
  1112.       xpos= (stdwidth  - wide)/ 2;
  1113.       ypos= (stdheight - high)/ 2;
  1114.     }
  1115.   }
  1116.  
  1117.   if(xpos<0)xpos=0;if(ypos<0)ypos=0;
  1118.   if(debugxemul)
  1119.     printf("opening screen w %d h %d d %d id %x overscan %d\n",wide,high,depth,id,overscan);
  1120.   if(!(Scr=OpenScreenTags(NULL,SA_Left,xpos,
  1121.               SA_Top,0, /*ypos,*/
  1122.               SA_Overscan,  overscan,
  1123.               SA_Width,    wide,
  1124.               SA_Height,    high,
  1125.               SA_Depth,    depth,
  1126.               SA_AutoScroll,1,
  1127. /*              SA_Interleaved, TRUE,*/
  1128.               SA_ShowTitle, FALSE,
  1129. /*              SA_Font,    &topaz8,*/
  1130.               SA_Type,    SCREENQUIET,
  1131.                     SA_DisplayID,    id,
  1132.               SA_Pens,    &DriPens[0],
  1133.               SA_Title,    LibX11Info,
  1134.               TAG_DONE )))
  1135.     return( SCREENOPENFAIL );
  1136.   
  1137.   if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE ))) X11resource_exit(DISPLAY8);
  1138.   
  1139. /*  rast=&(Scr->RastPort);*/
  1140. /*
  1141.   ret=OpenBackdropWindow(xpos,ypos,wide,high);
  1142.   if(ret!=0) return(SCRWINOPENFAIL);
  1143. */
  1144.   DG.nDisplayWidth=wide;
  1145.   DG.nDisplayHeight=high;
  1146.   DG.nDisplayDepth=depth;
  1147.   amiga_id=id;
  1148.   amiga_screen[0].cmap=(Colormap)Scr->ViewPort.ColorMap;
  1149. /*
  1150.   {
  1151.     int n;
  1152.     n=ObtainPen(amiga_screen[0].cmap,-1,SCALE8TO32(10),SCALE8TO32(10),SCALE8TO32(10),0);
  1153.     if(n==-1) printf("didn't work!\n");
  1154.     else ReleasePen(amiga_screen[0].cmap,n);
  1155.   }
  1156. */
  1157.   if(temprp.BitMap) free_bitmap(temprp.BitMap);
  1158.   else InitRastPort(&temprp);
  1159.   temprp.BitMap=alloc_bitmap(DG.nDisplayWidth+16,1,DG.nDisplayDepth,BMF_CLEAR);
  1160.   temprp.Layer=NULL;
  1161.  
  1162.   X11updatecmap();
  1163.   return(NULL);
  1164. }
  1165.  
  1166. void CloseDownScreen( void ){
  1167.   if(!Scr) return;
  1168. /*  CloseBackdropWindow();*/
  1169.   if ( VisualInfo ) {
  1170.     FreeVisualInfo( VisualInfo );
  1171.     VisualInfo = NULL;
  1172.   }
  1173.   if ( Scr && Scr!=wb /*!usewb&&!wbapp*/ ) {
  1174.     CloseScreen( Scr );
  1175.     Scr = NULL;
  1176.   }
  1177. }
  1178.  
  1179. /*
  1180. int OpenBackdropWindow( left,top,width,height ){
  1181.   if (!(rootwin = 
  1182.     OpenWindowTags( NULL,
  1183.                WA_Left,0,WA_Top,0,
  1184.                WA_Width,width,WA_Height,height,
  1185.                WA_IDCMP,IDCMP_INACTIVEWINDOW|IDCMP_ACTIVEWINDOW|IDCMP_NEWSIZE|
  1186.                IDCMP_MOUSEMOVE|IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW|
  1187.                IDCMP_REFRESHWINDOW|IDCMP_RAWKEY,
  1188.                WA_Flags,WFLG_NOCAREREFRESH|WFLG_SMART_REFRESH|WFLG_BACKDROP|
  1189.                WFLG_REPORTMOUSE|WFLG_BORDERLESS|WFLG_RMBTRAP,
  1190.                WA_ScreenTitle,    "TP was here..",
  1191.                WA_CustomScreen,    Scr,
  1192.                TAG_DONE ))) return( 4L );
  1193.   
  1194.   GT_RefreshWindow( rootwin, NULL );
  1195.   init_area(rootwin,MAX_COORD,rootwin->Width,rootwin->Height);
  1196.   return( 0L );
  1197. }
  1198.  
  1199.  
  1200. void CloseBackdropWindow(void){
  1201.   if (rootwin) {
  1202.     unclipWindow(rootwin->WLayer);
  1203.     CG.pPreviousLayer=NULL;
  1204.     exit_area(rootwin);
  1205.     XFlush(NULL);
  1206. /*    if(usewb) XDestroyWindow(&amigaX_display,rootwin);
  1207.     else*/ 
  1208.     CloseWindow(rootwin);
  1209.     rootwin = NULL;
  1210.   }
  1211. }
  1212. */
  1213. void init_backrp(width,height,depth){
  1214.   if(DG.bNeedBackRP){
  1215.     if(backrp.BitMap) free_bitmap(backrp.BitMap);
  1216.     else InitRastPort(&backrp);
  1217.     backrp.BitMap=alloc_bitmap(width,height,depth,BMF_CLEAR);
  1218.     backrp.Layer=NULL;
  1219.     if(backrp.BitMap) SetRast(&backrp,(UBYTE)0);
  1220.   }
  1221. }
  1222.  
  1223. Window AmigaCreateWindow(int wide,int high,int depth,int flag,ULONG id){
  1224. /*  int posx,posy;*/
  1225. #ifdef DEBUGXEMUL_ENTRY
  1226.   printf("AmigaCreateWindow %d %d %d (%d %d)\n",wide,high,depth,DG.nDisplayWidth,DG.nDisplayHeight);
  1227. #endif
  1228.   if(debugxemul)
  1229.     printf("createwindow w %d h %d d %d flag %d id %x\n",wide,high,depth,flag,id);
  1230.   if(usewb&&flag&&wb->RastPort.BitMap->Depth<=8){
  1231.   }
  1232. /*
  1233.   if(usewb){
  1234.     Window newwin;
  1235.   if(wide<=Scr->Width&&high<=Scr->Height&&depth<=Scr->RastPort.BitMap->Depth){
  1236.       posx=0; posy=0;
  1237.       if(rootwin){
  1238.     if(wide==DG.nDisplayWidth&&high==DG.nDisplayHeight) return(0);
  1239.     posx=rootwin->LeftEdge;
  1240.     posy=rootwin->TopEdge;
  1241.     unclipWindow(rootwin->WLayer);
  1242.     exit_area(rootwin);
  1243.     CG.pPreviousLayer=NULL;
  1244.     XFlush(NULL);
  1245.     CloseWindow(rootwin);
  1246.       }
  1247.       newwin=XCreateSimpleWindow(&amigaX_display,NULL,posx,posy,wide,high,0,0,0);
  1248.       if(!newwin) {return(ACWFAIL);}
  1249.       XMapWindow(NULL,newwin);
  1250.       rootwin=X11DrawablesWindows[X11DrawablesMap[newwin]];
  1251.       EG.fwindowsig=1<<rootwin->UserPort->mp_SigBit;
  1252. /*
  1253.       amiga_screen[0].root=newwin;
  1254. */
  1255.       DG.nDisplayWidth=wide;
  1256.       DG.nDisplayHeight=high;
  1257.       DG.nDisplayDepth=depth;
  1258.       adjx=rootwin->BorderLeft+rootwin->BorderRight;
  1259.       adjy=rootwin->BorderTop+rootwin->BorderBottom;
  1260.       if(borderadj==0)borderadj=1;
  1261.       amiga_id=GetVPModeID(&(wb->ViewPort));
  1262.       return(newwin);
  1263.     }
  1264.     prevcm=-1;
  1265.     usewb=0;
  1266.     wbapp=0;
  1267.    } 
  1268. */
  1269.  
  1270.   if(wb){
  1271.     wbmode=GetVPModeID(&(wb->ViewPort));
  1272.     wbmode=wbmode & MONITOR_ID_MASK;
  1273.   }
  1274.   if(!id){
  1275.     if(DG.bUse30){
  1276.       id= BestModeID(BIDTAG_NominalWidth, (UWORD)wide,
  1277.              BIDTAG_NominalHeight,(UWORD)high,
  1278.              BIDTAG_MonitorID , wbmode,
  1279.              flag ? BIDTAG_DIPFMustHave : TAG_IGNORE, flag,
  1280.              BIDTAG_Depth, (UBYTE)depth,
  1281.              TAG_END);
  1282.       if(debugxemul) printf("BestModeID to <%d,%d> is %x\n",wide,high,id);
  1283.       if(wide<=320){
  1284. /*    if(debugxemul) printf("setting lores\n");*/
  1285.     id=id&(0xFFFFFFFF-HIRES_KEY);
  1286.       }
  1287.     }else{
  1288.       id=0;
  1289.       if(depth<=4) if(wide>400)id|=HIRES_KEY;
  1290.       if(high>320) id|=LORESLACE_KEY;
  1291.     }
  1292.   }
  1293.   if((newpic&&id==amiga_id||!newpic)&&wide<=DG.nDisplayWidth&&high<=DG.nDisplayHeight&&Scr&&(amiga_ham8&&flag||!flag&&!amiga_ham8)){
  1294.     return(NULL);
  1295.   }
  1296.   if(Scr!=wb&&Scr) CloseDownScreen();
  1297.   return((Window)SetupScreen(wide,high,depth,id));
  1298. }
  1299.  
  1300. int gettimeofday(struct timeval *tp,struct timezone *tzp){
  1301. /*  long t;*/
  1302.   unsigned int clock[2];
  1303.   int x=timer(clock);
  1304. /*  if(!TimerBase)open_timer();
  1305.   GetSysTime(tp);*/
  1306. /*
  1307.   time(&t);
  1308.   tp->tv_sec=t;
  1309.   tp->tv_usec=0;
  1310. */
  1311.   if(!x){
  1312.     tp->tv_sec=(ULONG)clock[0];
  1313.     tp->tv_usec=(ULONG)clock[1];
  1314.   }else{
  1315.     tp->tv_sec=0;
  1316.     tp->tv_usec=0;
  1317.   }
  1318.   return(0);
  1319. }
  1320.  
  1321. /** mui part */
  1322.  
  1323. /* amigaspecific */
  1324.  
  1325. XNoOp(display)
  1326.      Display *display;
  1327. {/*                   File 'class4.o'*/
  1328. #ifdef DEBUGXEMUL
  1329. /*  printf("XNoOp\n");*/
  1330. #endif
  1331.   return(0);
  1332. }
  1333.  
  1334. XBell(Display *d,int n){
  1335. /*  printf("XBell..\n");*/
  1336.   printf("%c",7);
  1337. }
  1338.  
  1339. int XDisplayHeight(display, screen_number)
  1340.      Display *display;
  1341.      int screen_number;
  1342. {
  1343. #ifdef DEBUGXEMUL_ENTRY
  1344.   printf("XDisplayHeight\n");
  1345. #endif
  1346.   return(wb->Height);
  1347. }
  1348.  
  1349. int XDisplayWidth(display, screen_number)
  1350.      Display *display;
  1351.      int screen_number;
  1352. {
  1353. #ifdef DEBUGXEMUL_ENTRY
  1354.   printf("XDisplayWidth\n");
  1355. #endif
  1356.   return(wb->Width);
  1357. }
  1358.  
  1359. int XDisplayCells(display, screen_number)
  1360.      Display *display;
  1361.      int screen_number;
  1362. {
  1363. #ifdef DEBUGXEMUL_ENTRY
  1364.   printf("XDisplayCells\n");
  1365. #endif
  1366.   return(wb->BitMap.Depth);
  1367. }
  1368.  
  1369. int bReadDefaultsFile=0;
  1370.  
  1371. char *XGetDefault(display, program, option)
  1372.      Display *display;
  1373.      char *program;
  1374.      char *option;
  1375. {
  1376.   FILE *fp;
  1377.   XrmValue xv;
  1378. #if DEBUGXEMUL_ENTRY
  1379.   printf("XGetDefault\n");
  1380. #endif
  1381.   if(!bReadDefaultsFile){
  1382.     fp=fopen(".XDefaults","r");
  1383.     if(fp){
  1384.       X11ScanFile(fp);
  1385.       fclose(fp);
  1386.       XrmGetResource(XtDatabase(NULL),option,program,NULL,&xv);
  1387.       return((char*)xv.addr);
  1388.     }
  1389.     fclose(fp);
  1390.   } else {
  1391.     XrmGetResource(XtDatabase(NULL),option,program,NULL,&xv);
  1392.     return((char*)xv.addr);
  1393.   }
  1394.   return(NULL);
  1395. }
  1396.  
  1397. Status XQueryBestCursor(display, d, width, height,
  1398.             width_return, height_return)
  1399.      Display *display;
  1400.      Drawable d;
  1401.      unsigned int width, height;
  1402.      unsigned int *width_return, *height_return;
  1403. {
  1404. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  1405.   printf("WARNING: XQueryBestCursor\n");
  1406. #endif
  1407. }
  1408.  
  1409. Colormap XDefaultColormap(Display *d,int n){/*        File 'xast.o'*/
  1410. #ifdef DEBUGXEMUL_ENTRY
  1411.   printf("XDefaultColormap\n");
  1412. #endif
  1413.   assert(Scr);
  1414.  
  1415.   return((Colormap)Scr->ViewPort.ColorMap/*amiga_screen[n].cmap*/);
  1416. }
  1417.  
  1418. int X11check_size(Window,int,int);
  1419.  
  1420. int X11check_size(Window win,int width,int height){
  1421.   struct Window *w=NULL;
  1422.   if(X11Drawables[win]==X11SUBWINDOW){ /* this is a child */
  1423.     int parent=X11findparent(win);
  1424.     w=X11DrawablesWindows[X11DrawablesMap[parent]];
  1425.   } else if(X11Drawables[win]==X11WINDOW){
  1426.     w=X11DrawablesWindows[X11DrawablesMap[win]];
  1427.   } else if(X11Drawables[win]==X11MUI) {
  1428.     w=_window(X11DrawablesMUI[X11DrawablesMap[win]]);
  1429.   }
  1430.   if(!w) return 0;
  1431.   Xuserdata=(X11userdata *)w->UserData;
  1432.   if(width>Xuserdata->AWidth||height>Xuserdata->AHeight){
  1433. /*
  1434.     int max=Xuserdata->max_coords;
  1435.     exit_area(w);
  1436.     init_area(w,max,width,height);*/
  1437.     return 1;
  1438.   }
  1439.   return 0;
  1440. }
  1441.  
  1442. void _Xfillfit(Window,int);
  1443.  
  1444. void _Xfillfit(Window win,int n){
  1445.   struct Window *w=NULL;
  1446.   if(X11Drawables[win]==X11SUBWINDOW){ /* this is a child */
  1447.     int parent=X11findparent(win);
  1448.     w=X11DrawablesWindows[X11DrawablesMap[parent]];
  1449.   } else if(X11Drawables[win]==X11WINDOW){
  1450.     w=X11DrawablesWindows[X11DrawablesMap[win]];
  1451.   } else if(X11Drawables[win]==X11MUI) {
  1452.     w=_window(X11DrawablesMUI[X11DrawablesMap[win]]);
  1453.   } else if(X11Drawables[win]==X11BITMAP){
  1454.   }
  1455.   if(!w) return;
  1456. #if 0
  1457.   Xuserdata=(X11userdata *)w->UserData;
  1458. #endif
  1459.   if(n>Xuserdata->max_coords){
  1460.     exit_area(w);
  1461.     init_area(w,n+50,w->Width,w->Height);
  1462.     prevwin=-1;
  1463.   }
  1464. }
  1465.  
  1466. int oldX11FillSource,oldw,oldh;
  1467.  
  1468. void X11Setup_InternalFill(Pixmap pm){
  1469.   if(X11FillFree) free_bitmap(X11FillBitMap);
  1470.   X11FillSource=pm;
  1471.   X11FillBitMap=X11DrawablesBitmaps[X11DrawablesMap[pm]].pBitMap;
  1472.   X11FillFree=False;
  1473.   oldX11FillSource=0;
  1474. }
  1475.  
  1476. void X11Fit_InternalFill( int w, int h ){
  1477.   int x,y,nItemsX,nItemsY;
  1478.   int width;
  1479.   int height;
  1480.   struct BitMap *Source;
  1481.  
  1482.   if(oldX11FillSource==X11FillSource && oldw==w && oldh==h)
  1483.     return;
  1484.   oldX11FillSource=X11FillSource;
  1485.   oldw=w;
  1486.   oldh=h;
  1487.  
  1488.   width=X11DrawablesBitmaps[X11DrawablesMap[X11FillSource]].width;
  1489.   height=X11DrawablesBitmaps[X11DrawablesMap[X11FillSource]].height;
  1490.   Source=X11DrawablesBitmaps[X11DrawablesMap[X11FillSource]].pBitMap;
  1491.  
  1492.   nItemsX=(int)((w+width-1)/width);
  1493.   nItemsY=(int)((h+height-1)/height);
  1494.  
  1495.   if(width==w && height==h){
  1496.     init_backfillrp(width,height);
  1497.     return;
  1498.   } 
  1499.  
  1500.   init_backfillrp((nItemsX+1)*width,(nItemsY+1)*height);
  1501.   /* need to expand the source pattern */
  1502.  
  1503.   if(X11FillFree) free_bitmap(X11FillBitMap);
  1504.   X11FillFree=True;
  1505.   X11FillBitMap=alloc_bitmap((nItemsX+1)*width,(nItemsY+1)*height,1,BMF_CLEAR);
  1506.   for(x=1;x<8;x++){
  1507.     Source->Planes[x]=0;
  1508.     backfillrp.BitMap->Planes[x]=0;
  1509.   }
  1510.   for(y=0;y<nItemsY;y++)
  1511.     for(x=0;x<nItemsX;x++)
  1512.       BltBitMap(Source,0,0,X11FillBitMap,x*width,y*height,width,height,0xc0 /*(ABC|ABNC|ANBC)*/,0xff,NULL);
  1513. }
  1514.  
  1515. void init_backfillrp(width,height){
  1516.   if(backfillrp.BitMap){
  1517.     if(backfillrp.BitMap->BytesPerRow*8==width && backfillrp.BitMap->Rows==height){
  1518.       SetRast(&backfillrp,(UBYTE)0);
  1519.       return;
  1520.     }
  1521.     free_bitmap(backfillrp.BitMap);
  1522.     free_bitmap(backfillrp2.BitMap);
  1523.   }
  1524.   else {
  1525.     InitRastPort(&backfillrp);
  1526.     InitRastPort(&backfillrp2);
  1527.   }
  1528.   backfillrp.BitMap=alloc_bitmap(width,height,1,BMF_CLEAR);
  1529.   backfillrp2.BitMap=alloc_bitmap(width,height,1,BMF_CLEAR);
  1530.   backfillrp.Layer=NULL;
  1531.   backfillrp2.Layer=NULL;
  1532.   backfillrp.TmpRas=&(Xuserdata /*XBackUserData*/->win_tmpras);
  1533.   backfillrp.AreaInfo=&(Xuserdata /*XBackUserData*/->win_AIstruct);
  1534.   if(backfillrp.BitMap) SetRast(&backfillrp,(UBYTE)0);
  1535.   if(backfillrp2.BitMap) SetRast(&backfillrp,(UBYTE)0);
  1536. }
  1537.  
  1538. void setup_gc(GC gc){
  1539.   extern WORD *Xcurrent_tile;
  1540.   extern int Xtile_size,Xhas_tile;
  1541.   int   FillStyle=gc->values.fill_style&0xff;
  1542.   int   FillOp=gc->values.fill_style &0xff00;
  1543.   assert(drp);
  1544.   if(gc->values.function==GXinvert||gc->values.function==GXxor)
  1545.     SetDrMd(drp,COMPLEMENT);
  1546.   else/* if(gc->values.function==GXclear)*/
  1547.     SetDrMd(drp,JAM1);
  1548.  
  1549.   if(FillStyle==FillTiled||FillStyle==FillStippled||FillStyle==FillOpaqueStippled){
  1550.     SetDrMd(drp,JAM1);
  1551.     if(FillOp==NORMAL_FILL){
  1552.       X11Setup_Tile(gc,gc->values.tile);
  1553.       XSetTile(NULL,gc,gc->values.tile);
  1554.       SetAfPt(drp,Xcurrent_tile,Xtile_size);
  1555.       X11InternalFill=0;
  1556.     } else {
  1557.       SetAfPt(drp,0,0);
  1558.       X11Setup_InternalFill(gc->values.tile);
  1559.       X11InternalFill=1;
  1560.     }
  1561.   } else{
  1562.     SetAfPt(drp,0,0);
  1563.     X11InternalFill=0;
  1564.   }
  1565. /*  if(gc->values.line_width!=0){
  1566.     printf("linewidth %d\n",gc->values.line_width);
  1567.     w->RPort->PenWidth=gc->values.line_width;
  1568.     w->RPort->PenHeight=gc->values.line_width;
  1569.   }
  1570.   rpfill=drp;*/
  1571.   if(gc->values.line_style==LineSolid){
  1572.     SetDrPt(drp,0xFFFF);
  1573.   } else if(gc->values.line_style==LineOnOffDash ||
  1574.         gc->values.line_style==LineDoubleDash){
  1575.     SetDrPt(drp,Xdash); /* we're faking dashing.. */
  1576.   }
  1577.   SetAPen(drp,gc->values.foreground);
  1578.   SetBPen(drp,gc->values.background);
  1579.  
  1580.   prevgc=gc;
  1581. }
  1582.  
  1583. struct RastPort *setup_win(Window win){
  1584.   struct Window *w;
  1585.   if( win==ROOTID ){ /* oops drawing to root! */
  1586.     drp=&wb->RastPort;
  1587.     X_width=DG.nDisplayWidth;
  1588.     X_height=DG.nDisplayHeight;
  1589.     X_relx=0;
  1590.     X_rely=0;
  1591.   } else if(X11Drawables[win]==X11SUBWINDOW||X11Drawables[win]==X11WINDOW){
  1592.     w=Agetwin(win);
  1593.     prevwin=-1;
  1594.     if(!w){
  1595.       prevwin=-1;
  1596.       return 0;
  1597.     }
  1598.     drp=w->RPort;
  1599. /*
  1600.     X_width=w->Width;
  1601.     X_height=w->Height;
  1602. */
  1603.   } else if(X11Drawables[win]==X11BITMAP) {
  1604. #if 0
  1605.     w=Agetwin(1);
  1606. #endif
  1607.     setup_bitmap(win);
  1608.   } else { /* a mui object */
  1609.     Object *mwin=X11DrawablesMUI[X11DrawablesMap[win]];
  1610.     drp=_rp(mwin);
  1611.     X_relx=_left(mwin);
  1612.     X_rely=_top(mwin);
  1613.     X_width=_mwidth(mwin);
  1614.     X_height=_mheight(mwin);
  1615.   }
  1616.  
  1617.   prevwin=win;
  1618.   return(drp);
  1619. }
  1620.  
  1621. Screen *XDefaultScreenOfDisplay(Display *d){/* File 'image_f_io.o'*/
  1622.   Screen *scr=d->screens;
  1623. #ifdef DEBUGXEMUL_ENTRY
  1624.   printf("XDefaultScreenOfDisplay\n");
  1625. #endif
  1626.   return(&scr[0]);
  1627. }
  1628.  
  1629. int XDefaultDepth(
  1630.     Display *dpy          /* display */,
  1631.     int    scr        /* screen_number */
  1632. )
  1633. {
  1634.   return(DefaultDepth(dpy,scr));
  1635. }
  1636.  
  1637. int XDefaultScreen(
  1638.     Display *dpy          /* display */
  1639. )
  1640. {
  1641.   return(DefaultScreen(dpy));
  1642. }
  1643.  
  1644. extern GC XDefaultGC(
  1645.     Display *dpy          /* display */,
  1646.     int    scr        /* screen_number */
  1647. )
  1648. {
  1649.   return(DefaultGC(dpy,scr));
  1650. }
  1651.  
  1652. char *XDisplayString(
  1653.     Display *dpy        /* display */
  1654. )
  1655. {
  1656.   return(DisplayString(dpy));
  1657. }
  1658.  
  1659. XAutoRepeatOn(display)
  1660.      Display *display;
  1661. {}
  1662.  
  1663. unsigned long XBlackPixel(display, screen_number)
  1664.      Display *display;
  1665.      int screen_number;
  1666. {/*             File 'magick/libMagick.lib' */
  1667. #ifdef DEBUGXEMUL_ENTRY
  1668.   printf("XBlackPixel\n");
  1669. #endif
  1670.   return(BlackPixel(display,screen_number));
  1671. }
  1672.  
  1673. unsigned long XWhitePixel(display, screen_number)
  1674.      Display *display;
  1675.      int screen_number;
  1676. {/*             File 'magick/libMagick.lib' */
  1677. #ifdef DEBUGXEMUL_ENTRY
  1678.   printf("XWhitePixel\n");
  1679. #endif
  1680.   return(WhitePixel(display,screen_number));
  1681. }
  1682.  
  1683. Window XRootWindow(display, screen_number)
  1684.      Display *display;
  1685.      int screen_number;
  1686. {/*             File 'magick/libMagick.lib' */
  1687. #ifdef DEBUGXEMUL_ENTRY
  1688.   printf("XRootWindow\n");
  1689. #endif
  1690.   return(RootWindow(display,screen_number));
  1691. }
  1692.  
  1693. Window XRootWindowOfScreen(Screen *screen)
  1694. {
  1695.   return(screen->root);
  1696. }
  1697.  
  1698. unsigned long XBlackPixelOfScreen(screen)
  1699.      Screen *screen;
  1700. {
  1701.   return(screen->black_pixel);
  1702. }
  1703.  
  1704. void init_area(struct Window *win,int size,int w,int h){
  1705.   X11userdata *ud;
  1706.  
  1707. #ifdef DEBUGXEMUL
  1708.   printf("init_area %d\n",win);
  1709. #endif
  1710.  
  1711.   if( !Xuserdata ){
  1712.     ud=malloc(sizeof(X11userdata));
  1713.     if(!ud)  X11resource_exit(WINDOW3);
  1714.  
  1715.     ud->AWidth=w;
  1716.     ud->AHeight=h;
  1717.     
  1718.     ud->win_rastptr = (PLANEPTR)AllocRaster( ud->AWidth, ud->AHeight );
  1719.     if(!ud->win_rastptr) X11resource_exit(WINDOW1);
  1720.     
  1721.     InitTmpRas( &(ud->win_tmpras), ud->win_rastptr,((ud->AWidth+15)/16)*ud->AHeight );
  1722.     
  1723.     memset(&(ud->win_AIstruct),0,sizeof(struct AreaInfo));
  1724.     
  1725.     ud->coor_buf=malloc(size*5*sizeof(WORD));
  1726.     if(!ud->coor_buf)  X11resource_exit(WINDOW2);
  1727.     ud->max_coords=size;
  1728.     InitArea( &(ud->win_AIstruct), ud->coor_buf,size);
  1729.     Xuserdata=ud;
  1730.   } else 
  1731.     ud=Xuserdata;
  1732.  
  1733.   if(win){
  1734.     _Xoldarea=(win->RPort)->AreaInfo;
  1735.     _Xoldtmpras=(win->RPort)->TmpRas;
  1736.     (win->RPort)->TmpRas=&(ud->win_tmpras);
  1737.     (win->RPort)->AreaInfo = &(ud->win_AIstruct);
  1738.     win->UserData=(UBYTE*)ud;
  1739.   }
  1740. }
  1741.  
  1742. void X11free_userdata(X11userdata *ud){
  1743.   FreeRaster( ud->win_rastptr, ud->AWidth,ud->AHeight);
  1744.   if(ud->coor_buf)free(ud->coor_buf);
  1745.   free(ud);
  1746. }
  1747.  
  1748. void exit_area(struct Window *win){ 
  1749.   X11userdata *ud;
  1750. #ifdef DEBUGXEMUL_ENTRY
  1751.   printf("exit_area [%d]\n",win);
  1752. #endif
  1753.   ud=(X11userdata*)win->UserData;
  1754.   if(!ud) return;
  1755. #if 0
  1756.   Xuserdata=NULL;
  1757. #endif
  1758.  
  1759.   (win->RPort)->TmpRas=_Xoldtmpras;
  1760.   (win->RPort)->AreaInfo=_Xoldarea;
  1761.  
  1762. #if 0
  1763.   X11free_userdata(ud);
  1764. #endif
  1765.   win->UserData=NULL;
  1766. }
  1767.  
  1768. void SetBackground(int n){
  1769.   Xuserdata->background=n; 
  1770. }
  1771.  
  1772. GetBackground(struct Window *w){
  1773.   X11userdata *Xud;
  1774.   Xud=(X11userdata*)(w->UserData);
  1775.   return(Xud->background);
  1776. }
  1777.  
  1778. XSetWindowBackground(display, w, background_pixel)
  1779.      Display *display;
  1780.      Window w;
  1781.      unsigned long background_pixel;
  1782. {/*    File 'xvctrl.o' */
  1783. #if DEBUGXEMUL_ENTRY
  1784.   printf("XSetWindowBackground\n");
  1785. #endif
  1786.   X11DrawablesBackground[w]=(char)background_pixel;
  1787.   return(0);
  1788. }
  1789.